package cn.wuxing.mediation.businessplatform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.wuxing.mediation.businessplatform.domain.annotation.BusinessTag;
import cn.wuxing.mediation.businessplatform.domain.constants.BusinessListConstants;
import cn.wuxing.mediation.businessplatform.domain.dto.DelDTO;
import cn.wuxing.mediation.businessplatform.domain.dto.PreLitigationMediationEventOperateDTO;
import cn.wuxing.mediation.businessplatform.domain.enums.EventCategoryEnum;
import cn.wuxing.mediation.businessplatform.domain.pojo.EventOperateVO;
import cn.wuxing.mediation.businessplatform.domain.vo.BaseBusinessDetailVO;
import cn.wuxing.mediation.businessplatform.domain.vo.PreLitigationMediationEventDetailVO;
import cn.wuxing.mediation.businessplatform.mapper.business.AllCaseDataMapper;
import cn.wuxing.mediation.businessplatform.mapper.business.CmPreLitigationMediationEventMapper;
import cn.wuxing.mediation.common.core.domain.entity.*;
import cn.wuxing.mediation.common.core.redis.RedisCache;
import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.common.utils.SecurityUtils;
import cn.wuxing.mediation.common.utils.des.DESUtil;
import cn.wuxing.mediation.system.domain.SysDisputeType;
import cn.wuxing.mediation.system.mapper.SysDisputeTypeMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.ZoneId;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: <描述>
 * @date: 2024/9/15 21:07
 * @author: yang
 * @version: 1.0
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
@BusinessTag(tag = BusinessListConstants.PRE_LITIGATION_MEDIATION_EVENT)
public class PreLitigationMediationEventOperateServiceImpl extends
        BaseBusinessOperateServiceImpl<PreLitigationMediationEventOperateDTO, PreLitigationMediationEventDetailVO> {

    @Autowired
    private CmPreLitigationMediationEventMapper cmPreLitigationMediationEventMapper;

    @Autowired
    private SysDisputeTypeMapper sysDisputeTypeMapper;

    @Autowired
    private AllCaseDataMapper allCaseDataMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String PREFIX_CODE = "SQTJ";
    private AllCaseData getAllCaseData(PreLitigationMediationEventOperateDTO dto, CmPreLitigationMediationEvent entity) {
        AllCaseData allCaseData = new AllCaseData();
        allCaseData.setEventCode(entity.getEventCode());
        SysDisputeType sysDisputeType = this.sysDisputeTypeMapper.selectDisputeTypeByDisputeId(
                Long.parseLong(String.valueOf(dto.getEventType())));

        if (null != sysDisputeType) {
            allCaseData.setEventType(sysDisputeType.getDisputeName());
        }
        allCaseData.setEventDesc(entity.getEventDesc());
        allCaseData.setDsr(dto.getApplicantDTOS()==null||dto.getApplicantDTOS().isEmpty()?"":dto.getApplicantDTOS().stream().map(
                applicantDTO -> DESUtil.encrypt(applicantDTO.getName())).collect(Collectors.joining(",")));
        allCaseData.setAreaId(String.valueOf(dto.getAreaId()));
        allCaseData.setInitiateTime(entity.getCreateTime());
        allCaseData.setSource("智能矛调应用-诉前");
        allCaseData.setBusinessId(String.valueOf(entity.getId()));
        allCaseData.setEventCategory("3");
        allCaseData.setBusinessTypeName(EventCategoryEnum.PRE_MEDIATION_EVENT.getDesc());
        return allCaseData;
    }

    @Override
    Boolean delEvent(DelDTO dto) {
        CmPreLitigationMediationEvent cmPreLitigationMediationEvent = new CmPreLitigationMediationEvent();
        LambdaUpdateWrapper<CmPreLitigationMediationEvent> wrapper = new LambdaUpdateWrapper<>(
                cmPreLitigationMediationEvent);
        wrapper.in(CmPreLitigationMediationEvent::getId, dto.getIds());
        wrapper.set(CmPreLitigationMediationEvent::getDelFlag, "2");
        wrapper.set(CmPreLitigationMediationEvent::getDelTime, DateTime.now());
        AllCaseData allCaseData = new AllCaseData();
        allCaseData.setDeleted(2);
        LambdaUpdateWrapper<AllCaseData> allCaseDataLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        allCaseDataLambdaUpdateWrapper.in(AllCaseData::getBusinessId, dto.getIds());
        allCaseDataLambdaUpdateWrapper.eq(AllCaseData::getEventCategory, BusinessListConstants.PRE_LITIGATION_MEDIATION_EVENT);
        allCaseDataMapper.update(allCaseData, allCaseDataLambdaUpdateWrapper);
        return this.cmPreLitigationMediationEventMapper.update(cmPreLitigationMediationEvent, wrapper) > 0;
    }

    @Override
    EventOperateVO saveEvent(PreLitigationMediationEventOperateDTO dto) {
        EventOperateVO vo = new EventOperateVO();
        CmPreLitigationMediationEvent cmPreLitigationMediationEvent = BeanUtil.copyProperties(dto,
                CmPreLitigationMediationEvent.class);

        if (null == cmPreLitigationMediationEvent.getId()) {
            cmPreLitigationMediationEvent.setEventCode(this.redisCache.getCode(PREFIX_CODE));
            cmPreLitigationMediationEvent.setCreateBy(SecurityUtils.getNickName());
            cmPreLitigationMediationEvent.setCreateTime(DateTime.now());
            cmPreLitigationMediationEvent.setUpdateTime(DateTime.now());
            cmPreLitigationMediationEvent.setRegisterUserId(SecurityUtils.getUserId());
            cmPreLitigationMediationEvent.setCloseCaseTime(dto.getCloseCaseTime());
            this.cmPreLitigationMediationEventMapper.insert(cmPreLitigationMediationEvent);

            AllCaseData allCaseData = getAllCaseData(dto, cmPreLitigationMediationEvent);
            allCaseDataMapper.insert(allCaseData);
        } else {
            cmPreLitigationMediationEvent.setUpdateBy(SecurityUtils.getNickName());
            cmPreLitigationMediationEvent.setUpdateTime(DateTime.now());
            cmPreLitigationMediationEvent.setCloseCaseTime(dto.getCloseCaseTime());
            LambdaUpdateWrapper<CmPreLitigationMediationEvent> updateCmPreLitigationMediationEventWrapper = new LambdaUpdateWrapper<>();
            updateCmPreLitigationMediationEventWrapper.eq(CmPreLitigationMediationEvent::getId, dto.getId());
            if ("4".equals(dto.getOperateType())) {
                updateCmPreLitigationMediationEventWrapper.set(CmPreLitigationMediationEvent::getWindowId, null);
            }
            this.cmPreLitigationMediationEventMapper.update(cmPreLitigationMediationEvent,
                    updateCmPreLitigationMediationEventWrapper);
           /* cmPreLitigationMediationEvent = this.cmPreLitigationMediationEventMapper.selectById(dto.getId());
            AllCaseData allCaseData = getAllCaseData(dto, cmPreLitigationMediationEvent);
            LambdaUpdateWrapper<AllCaseData> updateAllCaseDataWrapper = new LambdaUpdateWrapper<>();
            updateAllCaseDataWrapper.eq(AllCaseData::getBusinessId, cmPreLitigationMediationEvent.getId());
            updateAllCaseDataWrapper.eq(AllCaseData::getEventCategory, BusinessListConstants.PRE_LITIGATION_MEDIATION_EVENT);
            allCaseDataMapper.update(allCaseData, updateAllCaseDataWrapper);*/
        }
        dto.setEventCategory(EventCategoryEnum.PRE_MEDIATION_EVENT.getCode());
        CmPreLitigationMediationEvent preLitigationMediationEvent = this.cmPreLitigationMediationEventMapper.selectById(
                cmPreLitigationMediationEvent.getId());
        if (null == preLitigationMediationEvent) {
            throw new ServiceException("未找到事件！");
        }
        vo.setId(preLitigationMediationEvent.getId());
        vo.setEventCode(preLitigationMediationEvent.getEventCode());
        vo.setEventState(preLitigationMediationEvent.getEventState());
        return vo;
    }

    @Override
    PreLitigationMediationEventDetailVO getEventInfoDetail(Long eventId) {
        CmPreLitigationMediationEvent cmPreLitigationMediationEvent = this.cmPreLitigationMediationEventMapper.selectById(
                eventId);

        if (null == cmPreLitigationMediationEvent) {
            return new PreLitigationMediationEventDetailVO();
        }
        PreLitigationMediationEventDetailVO preLitigationMediationEventDetailVO = BeanUtil.copyProperties(
                cmPreLitigationMediationEvent,
                PreLitigationMediationEventDetailVO.class);
        if (null != preLitigationMediationEventDetailVO.getEventType()) {
            SysDisputeType sysDisputeType = this.sysDisputeTypeMapper.selectDisputeTypeByDisputeId(
                    Long.parseLong(String.valueOf(preLitigationMediationEventDetailVO.getEventType())));

            if (null != sysDisputeType) {
                preLitigationMediationEventDetailVO.setEventTypeLabel(sysDisputeType.getDisputeName());
            }
        }
        preLitigationMediationEventDetailVO.setEventCategory(EventCategoryEnum.PRE_MEDIATION_EVENT.getCode());
        preLitigationMediationEventDetailVO.setEventSource(null != cmPreLitigationMediationEvent.getEventSource()?
                String.valueOf(cmPreLitigationMediationEvent.getEventSource()) : null);
        preLitigationMediationEventDetailVO.setRegisterUser(cmPreLitigationMediationEvent.getCreateBy());
        preLitigationMediationEventDetailVO.setRegisterTime(cmPreLitigationMediationEvent.getCreateTime());
        preLitigationMediationEventDetailVO.setCloseCaseTime(cmPreLitigationMediationEvent.getCloseCaseTime());
        return preLitigationMediationEventDetailVO;

    }

    @Override
    public void checkCloseCase(Long eventId) {
        BaseBusinessDetailVO eventDetail = super.getEventDetail(eventId);
        super.checkApplicant(eventDetail.getApplicantDetailVOList());
        if (null == eventDetail.getMediationRecordDetailVO()) {
            throw new ServiceException("请先填写调解记录!");
        }
        super.checkDossier(eventDetail.getDossierFilesVO(),
                eventDetail.getMediationRecordDetailVO().get(0).getMediationResult());
    }
}
